package com.yong.sbd.biz.mp.controller;

import com.yong.sbd.biz.mp.entity.SysUser;
import com.yong.sbd.biz.mp.service.SysUserService;
import com.yong.sbd.common.beans.page.PageParam;
import com.yong.sbd.common.beans.page.PageResult;
import com.yong.sbd.common.beans.response.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 *
 * @author Kang Yong
 * @date 2025/10/27
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class SysUserController {
    
    private final SysUserService sysUserService;

    /**
    * 分页
    * @author KangYong
    * @date 2025/10/28
    * @param pageParam {@link PageParam}
    * @return {@link R< PageResult< SysUser>>}
    */
    public R<PageResult<SysUser>> page(@RequestParam PageParam pageParam) {
        return R.success(sysUserService.pageData(pageParam));
    }
    
    /**
     * 查询所有用户
     *
     * @return {@link R<List<SysUser>>}
     */
    @GetMapping("/list")
    public R<List<SysUser>> list() {
        return R.success(sysUserService.list());
    }
    
    /**
     * 根据ID查询用户
     *
     * @param id 用户ID
     * @return {@link R<SysUser>}
     */
    @GetMapping("/{id}")
    public R<SysUser> getById(@PathVariable Long id) {
        return R.success(sysUserService.getById(id));
    }
    
    /**
     * 根据用户ID查询用户及其角色信息
     *
     * @param userId 用户ID
     * @return {@link R<SysUser>}
     */
    @GetMapping("/{userId}/with-roles")
    public R<SysUser> getUserWithRoles(@PathVariable Long userId) {
        return R.success(sysUserService.getUserWithRoles(userId));
    }
    
    /**
     * 查询所有用户及其角色信息
     *
     * @return {@link R<List<SysUser>>}
     */
    @GetMapping("/with-roles")
    public R<List<SysUser>> getUsersWithRoles() {
        return R.success(sysUserService.getUsersWithRoles());
    }
    
    /**
     * 添加用户
     *
     * @param sysUser 用户信息
     * @return {@link R<Boolean>}
     */
    @PostMapping
    public R<Boolean> save(@RequestBody SysUser sysUser) {
        return R.success(sysUserService.save(sysUser));
    }
    
    /**
     * 更新用户
     *
     * @param sysUser 用户信息
     * @return {@link R<Boolean>}
     */
    @PutMapping
    public R<Boolean> update(@RequestBody SysUser sysUser) {
        return R.success(sysUserService.updateById(sysUser));
    }
    
    /**
     * 删除用户
     *
     * @param id 用户ID
     * @return {@link R<Boolean>}
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.success(sysUserService.removeById(id));
    }
}